<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Mongo复制集,Mongo," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.0" />






<meta name="description" content="1、MongoDB 复制集MongoDB复制是将数据同步在多个服务器的过程。复制提供了数据的冗余备份，并在多个服务器上存储数据副本，提高了数据的可用性， 并可以保证数据的安全性。复制还允许您从硬件故障和服务中断中恢复数据。2、什么是复制集保障数据的安全性数据高可用性灾难恢复无需停机维护（如备份，重建索引，压缩）分布式读取数据3、MongoDB复制集原理mongodb的复制至少需要两个节点。其中一个">
<meta name="keywords" content="Mongo复制集,Mongo">
<meta property="og:type" content="article">
<meta property="og:title" content="Mongo复制集一">
<meta property="og:url" content="https://gitee.com/shuhao/2017/12/01/Mongo复制集一/index.html">
<meta property="og:site_name" content="Niki">
<meta property="og:description" content="1、MongoDB 复制集MongoDB复制是将数据同步在多个服务器的过程。复制提供了数据的冗余备份，并在多个服务器上存储数据副本，提高了数据的可用性， 并可以保证数据的安全性。复制还允许您从硬件故障和服务中断中恢复数据。2、什么是复制集保障数据的安全性数据高可用性灾难恢复无需停机维护（如备份，重建索引，压缩）分布式读取数据3、MongoDB复制集原理mongodb的复制至少需要两个节点。其中一个">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://gitee.com/blog/images/mongo/replication.png">
<meta property="og:updated_time" content="2017-12-04T08:45:15.859Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mongo复制集一">
<meta name="twitter:description" content="1、MongoDB 复制集MongoDB复制是将数据同步在多个服务器的过程。复制提供了数据的冗余备份，并在多个服务器上存储数据副本，提高了数据的可用性， 并可以保证数据的安全性。复制还允许您从硬件故障和服务中断中恢复数据。2、什么是复制集保障数据的安全性数据高可用性灾难恢复无需停机维护（如备份，重建索引，压缩）分布式读取数据3、MongoDB复制集原理mongodb的复制至少需要两个节点。其中一个">
<meta name="twitter:image" content="https://gitee.com/blog/images/mongo/replication.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/shuhao/2017/12/01/Mongo复制集一/"/>





  <title> Mongo复制集一 | Niki </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cb9f0585e406f878cb2f29c89ad3cdb7";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Niki</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">天道酬勤</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-categories"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/shuhao/blog/2017/12/01/Mongo复制集一/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fang ShuHao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/head.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Niki">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Mongo复制集一
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-01T17:41:35+08:00">
                2017-12-01
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/Mongo/" itemprop="url" rel="index">
                    <span itemprop="name">Mongo</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1、MongoDB-复制集"><a href="#1、MongoDB-复制集" class="headerlink" title="1、MongoDB 复制集"></a>1、MongoDB 复制集</h2><h6 id="MongoDB复制是将数据同步在多个服务器的过程。"><a href="#MongoDB复制是将数据同步在多个服务器的过程。" class="headerlink" title="MongoDB复制是将数据同步在多个服务器的过程。"></a>MongoDB复制是将数据同步在多个服务器的过程。</h6><h6 id="复制提供了数据的冗余备份，并在多个服务器上存储数据副本，提高了数据的可用性，-并可以保证数据的安全性。"><a href="#复制提供了数据的冗余备份，并在多个服务器上存储数据副本，提高了数据的可用性，-并可以保证数据的安全性。" class="headerlink" title="复制提供了数据的冗余备份，并在多个服务器上存储数据副本，提高了数据的可用性， 并可以保证数据的安全性。"></a>复制提供了数据的冗余备份，并在多个服务器上存储数据副本，提高了数据的可用性， 并可以保证数据的安全性。</h6><h6 id="复制还允许您从硬件故障和服务中断中恢复数据。"><a href="#复制还允许您从硬件故障和服务中断中恢复数据。" class="headerlink" title="复制还允许您从硬件故障和服务中断中恢复数据。"></a>复制还允许您从硬件故障和服务中断中恢复数据。</h6><h2 id="2、什么是复制集"><a href="#2、什么是复制集" class="headerlink" title="2、什么是复制集"></a>2、什么是复制集</h2><h6 id="保障数据的安全性"><a href="#保障数据的安全性" class="headerlink" title="保障数据的安全性"></a>保障数据的安全性</h6><h6 id="数据高可用性"><a href="#数据高可用性" class="headerlink" title="数据高可用性"></a>数据高可用性</h6><h6 id="灾难恢复"><a href="#灾难恢复" class="headerlink" title="灾难恢复"></a>灾难恢复</h6><h6 id="无需停机维护（如备份，重建索引，压缩）"><a href="#无需停机维护（如备份，重建索引，压缩）" class="headerlink" title="无需停机维护（如备份，重建索引，压缩）"></a>无需停机维护（如备份，重建索引，压缩）</h6><h6 id="分布式读取数据"><a href="#分布式读取数据" class="headerlink" title="分布式读取数据"></a>分布式读取数据</h6><h2 id="3、MongoDB复制集原理"><a href="#3、MongoDB复制集原理" class="headerlink" title="3、MongoDB复制集原理"></a>3、MongoDB复制集原理</h2><h6 id="mongodb的复制至少需要两个节点。其中一个是主节点，负责处理客户端请求，其余的都是从节点，负责复制主节点上的数据。"><a href="#mongodb的复制至少需要两个节点。其中一个是主节点，负责处理客户端请求，其余的都是从节点，负责复制主节点上的数据。" class="headerlink" title="mongodb的复制至少需要两个节点。其中一个是主节点，负责处理客户端请求，其余的都是从节点，负责复制主节点上的数据。"></a>mongodb的复制至少需要两个节点。其中一个是主节点，负责处理客户端请求，其余的都是从节点，负责复制主节点上的数据。</h6><h6 id="mongodb各个节点常见的搭配方式为：一主一从、一主多从。"><a href="#mongodb各个节点常见的搭配方式为：一主一从、一主多从。" class="headerlink" title="mongodb各个节点常见的搭配方式为：一主一从、一主多从。"></a>mongodb各个节点常见的搭配方式为：一主一从、一主多从。</h6><h6 id="主节点记录在其上的所有操作oplog，从节点定期轮询主节点获取这些操作，然后对自己的数据副本执行这些操作，从而保证从节点的数据与主节点一致。"><a href="#主节点记录在其上的所有操作oplog，从节点定期轮询主节点获取这些操作，然后对自己的数据副本执行这些操作，从而保证从节点的数据与主节点一致。" class="headerlink" title="主节点记录在其上的所有操作oplog，从节点定期轮询主节点获取这些操作，然后对自己的数据副本执行这些操作，从而保证从节点的数据与主节点一致。"></a>主节点记录在其上的所有操作oplog，从节点定期轮询主节点获取这些操作，然后对自己的数据副本执行这些操作，从而保证从节点的数据与主节点一致。</h6><p>MongoDB复制结构图如下所示：</p>
<img src="/blog/images/mongo/replication.png">
<h2 id="4、MongoDB复制集特征"><a href="#4、MongoDB复制集特征" class="headerlink" title="4、MongoDB复制集特征"></a>4、MongoDB复制集特征</h2><p>N 个节点的集群<br>任何节点可作为主节点<br>所有写入操作都在主节点上<br>自动故障转移<br>自动恢复</p>
<h2 id="5、复制集配置-（window64环境-Mongo3-4）"><a href="#5、复制集配置-（window64环境-Mongo3-4）" class="headerlink" title="5、复制集配置 （window64环境 ~ Mongo3.4）"></a>5、复制集配置 （window64环境 ~ Mongo3.4）</h2><h3 id="5-1-新建复制集目录"><a href="#5-1-新建复制集目录" class="headerlink" title="5.1 新建复制集目录"></a>5.1 新建复制集目录</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">D:\Program Soft\MongoDB\replicset\db_rs0</span><br><span class="line">D:\Program Soft\MongoDB\replicset\db_rs1</span><br><span class="line">D:\Program Soft\MongoDB\replicset\db_rs2</span><br></pre></td></tr></table></figure>
<h3 id="5-2-建立数据目录-以及日志目录"><a href="#5-2-建立数据目录-以及日志目录" class="headerlink" title="5.2 建立数据目录 以及日志目录"></a>5.2 建立数据目录 以及日志目录</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">D:\Program Soft\MongoDB\replicset\db_rs0\data</span><br><span class="line">D:\Program Soft\MongoDB\replicset\db_rs1\data</span><br><span class="line">D:\Program Soft\MongoDB\replicset\db_rs2\data</span><br><span class="line"></span><br><span class="line">D:\Program Soft\MongoDB\replicset\db_rs0\log</span><br><span class="line">D:\Program Soft\MongoDB\replicset\db_rs1\log</span><br><span class="line">D:\Program Soft\MongoDB\replicset\db_rs2\log</span><br></pre></td></tr></table></figure>
<p>在日志目录下分别新建三个日志文件： rs0_0.log 、rs0_1.log、rs0_2.log</p>
<h3 id="5-3-新建配置文件"><a href="#5-3-新建配置文件" class="headerlink" title="5.3 新建配置文件"></a>5.3 新建配置文件</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">D:\Program Soft\MongoDB\replicset\db_rs0\rs0_0.conf</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#配置文件内容</span><br><span class="line"></span><br><span class="line">dbpath = D:\Program Soft\MongoDB\replicset\db_rs0\data</span><br><span class="line">logpath = D:\Program Soft\MongoDB\replicset\db_rs0\log\rs0_0.log</span><br><span class="line">journal = true</span><br><span class="line">port = 40000 </span><br><span class="line">replSet = rs0</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">D:\Program Soft\MongoDB\replicset\db_rs0\rs0_1.conf</span><br><span class="line"></span><br><span class="line">#配置文件内容</span><br><span class="line"></span><br><span class="line">dbpath = D:\Program Soft\MongoDB\replicset\db_rs0\data</span><br><span class="line">logpath = D:\Program Soft\MongoDB\replicset\db_rs0\log\rs0_1.log</span><br><span class="line">journal = true</span><br><span class="line">port = 40001</span><br><span class="line">replSet = rs0</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">D:\Program Soft\MongoDB\replicset\db_rs0\rs0_2.conf</span><br><span class="line"></span><br><span class="line">#配置文件内容</span><br><span class="line"></span><br><span class="line">dbpath = D:\Program Soft\MongoDB\replicset\db_rs0\data</span><br><span class="line">logpath = D:\Program Soft\MongoDB\replicset\db_rs0\log\rs0_2.log</span><br><span class="line">journal = true</span><br><span class="line">port = 40002</span><br><span class="line">replSet = rs0</span><br></pre></td></tr></table></figure>
<p>配置文件参数：<br>dbpath：Mongo数据保存路径<br>logpath：Mongo日志保存路径<br>journal：是否开启Mongo日志（性能和安全方面角色。）<br>port：Mongo实例启动的端口<br>replSet：Mongo复制集名<br><a href="http://www.mongoing.com/docs/administration/replica-sets.html" target="_blank" rel="external">复制集配置指南参考</a></p>
<h2 id="6、启动复制集"><a href="#6、启动复制集" class="headerlink" title="6、启动复制集"></a>6、启动复制集</h2><h3 id="6-1-启动所有Mongo实例"><a href="#6-1-启动所有Mongo实例" class="headerlink" title="6.1 启动所有Mongo实例"></a>6.1 启动所有Mongo实例</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">mongod.exe -replSet rs0 -config &quot;D:\Program Soft\MongoDB\replicset\db_rs0\rs0_0.conf&quot;</span><br><span class="line">mongod.exe -replSet rs0 -config &quot;D:\Program Soft\MongoDB\replicset\db_rs1\rs0_1.conf&quot;</span><br><span class="line">mongod.exe -replSet rs0 -config &quot;D:\Program Soft\MongoDB\replicset\db_rs2\rs0_2.conf&quot;</span><br></pre></td></tr></table></figure>
<h4 id="注意"><a href="#注意" class="headerlink" title="注意"></a><em>注意</em></h4><h4 id="启动Mongo实例时，需要制定复制集参数-replSet-rsName-在配置文件中设置"><a href="#启动Mongo实例时，需要制定复制集参数-replSet-rsName-在配置文件中设置" class="headerlink" title="启动Mongo实例时，需要制定复制集参数-replSet rsName(在配置文件中设置)"></a>启动Mongo实例时，需要制定复制集参数-replSet rsName(在配置文件中设置)</h4><h3 id="6-2-启动实例客户端"><a href="#6-2-启动实例客户端" class="headerlink" title="6.2 启动实例客户端"></a>6.2 启动实例客户端</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">mongo -p 4000</span><br><span class="line">mongo -p 4001</span><br><span class="line">mongo -p 4002</span><br></pre></td></tr></table></figure>
<p>此时，我们的复制集还没有主从节点的划分，我们将rs0_0实例设置为主节点。此时在rs0_0的客户端执行复制集初始化命令。Mongo会将该实例设为复制集的primary节点<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">&gt; rs.initiate()</span><br><span class="line">&#123;</span><br><span class="line">        &quot;info2&quot; : &quot;no configuration specified. Using a default configuration for the set&quot;,</span><br><span class="line">        &quot;me&quot; : &quot;YZJC1606-0218:40000&quot;,</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">rs.conf() #查看复制集配置</span><br><span class="line"></span><br><span class="line">rs.status() #查看复制集状态</span><br></pre></td></tr></table></figure></p>
<h3 id="注意-1"><a href="#注意-1" class="headerlink" title="注意"></a><em>注意</em></h3><p>如果不调用initiate()方法设置主节点，则会出现如下异常<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">2017-12-02T14:59:59.649+0800 E QUERY    [thread1] Error: Could not retrieve replica set config: &#123;</span><br><span class="line">        &quot;info&quot; : &quot;run rs.initiate(...) if not yet done for the set&quot;,</span><br><span class="line">        &quot;ok&quot; : 0,</span><br><span class="line">        &quot;errmsg&quot; : &quot;no replset config has been received&quot;,</span><br><span class="line">        &quot;code&quot; : 94,</span><br><span class="line">        &quot;codeName&quot; : &quot;NotYetInitialized&quot;</span><br><span class="line">&#125; :</span><br></pre></td></tr></table></figure></p>
<h3 id="6-2-添加、删除从节点"><a href="#6-2-添加、删除从节点" class="headerlink" title="6.2 添加、删除从节点"></a>6.2 添加、删除从节点</h3><p>将rs0_0实例设置为主节点后，需要添加从节点<br>在rs0_0实例的客户端执行如下命令将另外两个节点设置为复制集的从节点<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">rs.add(&quot;YZJC1606-0218:40001&quot;)</span><br><span class="line">rs.add(&quot;YZJC1606-0218:40002&quot;)</span><br></pre></td></tr></table></figure></p>
<h3 id="注意-2"><a href="#注意-2" class="headerlink" title="注意"></a><em>注意</em></h3><p>rs.add的参数是节点名，默认的Mongo的节点名为【计算机名:port】</p>
<h3 id="6-3-在从节点中设置slaveOK"><a href="#6-3-在从节点中设置slaveOK" class="headerlink" title="6.3 在从节点中设置slaveOK"></a>6.3 在从节点中设置slaveOK</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">rs.slaveOk()</span><br></pre></td></tr></table></figure>
<p>在另外两个从节点的客户端调用slaveOK()方法设置，否则在从节点客户端查询数据时出现如下异常：<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">2017-12-02T14:05:37.986+0800 E QUERY    [thread1] Error: listDatabases failed:&#123;</span><br><span class="line">        &quot;ok&quot; : 0,</span><br><span class="line">        &quot;errmsg&quot; : &quot;not master and slaveOk=false&quot;,</span><br><span class="line">        &quot;code&quot; : 13435,</span><br><span class="line">        &quot;codeName&quot; : &quot;NotMasterNoSlaveOk&quot;</span><br><span class="line">&#125; :</span><br></pre></td></tr></table></figure></p>
<p>如果需要移除复制集中的从节点，则只需要在主节点实例的客户端调用如下命令<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">rs.remove(&quot;YZJC1606-0218:40002&quot;)</span><br><span class="line">#如需重新将从节点加入到复制集只需要重新调用add方法</span><br></pre></td></tr></table></figure></p>
<h3 id="主节点停机的情况："><a href="#主节点停机的情况：" class="headerlink" title="主节点停机的情况："></a>主节点停机的情况：</h3><p>因为Mongo复制集的自动恢复特性，如果集群中一个从节点挂掉，则调用rs.status可以看到该从节点health属性为0，如果该节点故障排除重新启动，会自动添加到复制集中来，并且health状态重新为1<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">rs1:PRIMARY&gt; rs.status()</span><br><span class="line">&#123;</span><br><span class="line">        &quot;set&quot; : &quot;rs1&quot;,</span><br><span class="line">        &quot;date&quot; : ISODate(&quot;2017-12-02T07:20:22.475Z&quot;),</span><br><span class="line">        &quot;myState&quot; : 1,</span><br><span class="line">        &quot;term&quot; : NumberLong(1),</span><br><span class="line">        &quot;heartbeatIntervalMillis&quot; : NumberLong(2000),</span><br><span class="line">        &quot;optimes&quot; : &#123;</span><br><span class="line">                &quot;lastCommittedOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(1)</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;appliedOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(1)</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;durableOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(1)</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;members&quot; : [</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 0,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50002&quot;,</span><br><span class="line">                        &quot;health&quot; : 1,</span><br><span class="line">                        &quot;state&quot; : 1,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;PRIMARY&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 1345,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;2017-12-02T07:20:12Z&quot;),</span><br><span class="line">                        &quot;electionTime&quot; : Timestamp(1512198160, 2),</span><br><span class="line">                        &quot;electionDate&quot; : ISODate(&quot;2017-12-02T07:02:40Z&quot;),</span><br><span class="line">                        &quot;configVersion&quot; : 3,</span><br><span class="line">                        &quot;self&quot; : true</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 1,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50001&quot;,</span><br><span class="line">                        &quot;health&quot; : 1,</span><br><span class="line">                        &quot;state&quot; : 2,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;SECONDARY&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 955,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDurable&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;2017-12-02T07:20:12Z&quot;),</span><br><span class="line">                        &quot;optimeDurableDate&quot; : ISODate(&quot;2017-12-02T07:20:12Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeat&quot; : ISODate(&quot;2017-12-02T07:20:20.927Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeatRecv&quot; : ISODate(&quot;2017-12-02T07:20:21.314Z&quot;),</span><br><span class="line">                        &quot;pingMs&quot; : NumberLong(0),</span><br><span class="line">                        &quot;syncingTo&quot; : &quot;YZJC1606-0218:50002&quot;,</span><br><span class="line">                        &quot;configVersion&quot; : 3</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 2,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50003&quot;,</span><br><span class="line">                        &quot;health&quot; : 0,</span><br><span class="line">                        &quot;state&quot; : 2,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;SECONDARY&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 1,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199172, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDurable&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199172, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;2017-12-02T07:19:32Z&quot;),</span><br><span class="line">                        &quot;optimeDurableDate&quot; : ISODate(&quot;2017-12-02T07:19:32Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeat&quot; : ISODate(&quot;2017-12-02T07:20:20.946Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeatRecv&quot; : ISODate(&quot;2017-12-02T07:19:38.290Z&quot;),</span><br><span class="line">                        &quot;pingMs&quot; : NumberLong(0),</span><br><span class="line">                        &quot;configVersion&quot; : 3</span><br><span class="line">                &#125;</span><br><span class="line">        ],</span><br><span class="line">        &quot;ok&quot; : 1</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">rs1:PRIMARY&gt; rs.status()</span><br><span class="line">&#123;</span><br><span class="line">        &quot;set&quot; : &quot;rs1&quot;,</span><br><span class="line">        &quot;date&quot; : ISODate(&quot;2017-12-02T07:20:22.475Z&quot;),</span><br><span class="line">        &quot;myState&quot; : 1,</span><br><span class="line">        &quot;term&quot; : NumberLong(1),</span><br><span class="line">        &quot;heartbeatIntervalMillis&quot; : NumberLong(2000),</span><br><span class="line">        &quot;optimes&quot; : &#123;</span><br><span class="line">                &quot;lastCommittedOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(1)</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;appliedOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(1)</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;durableOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(1)</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;members&quot; : [</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 0,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50002&quot;,</span><br><span class="line">                        &quot;health&quot; : 1,</span><br><span class="line">                        &quot;state&quot; : 1,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;PRIMARY&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 1345,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;2017-12-02T07:20:12Z&quot;),</span><br><span class="line">                        &quot;electionTime&quot; : Timestamp(1512198160, 2),</span><br><span class="line">                        &quot;electionDate&quot; : ISODate(&quot;2017-12-02T07:02:40Z&quot;),</span><br><span class="line">                        &quot;configVersion&quot; : 3,</span><br><span class="line">                        &quot;self&quot; : true</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 1,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50001&quot;,</span><br><span class="line">                        &quot;health&quot; : 1,</span><br><span class="line">                        &quot;state&quot; : 2,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;SECONDARY&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 955,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDurable&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199212, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;2017-12-02T07:20:12Z&quot;),</span><br><span class="line">                        &quot;optimeDurableDate&quot; : ISODate(&quot;2017-12-02T07:20:12Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeat&quot; : ISODate(&quot;2017-12-02T07:20:20.927Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeatRecv&quot; : ISODate(&quot;2017-12-02T07:20:21.314Z&quot;),</span><br><span class="line">                        &quot;pingMs&quot; : NumberLong(0),</span><br><span class="line">                        &quot;syncingTo&quot; : &quot;YZJC1606-0218:50002&quot;,</span><br><span class="line">                        &quot;configVersion&quot; : 3</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 2,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50003&quot;,</span><br><span class="line">                        &quot;health&quot; : 1,</span><br><span class="line">                        &quot;state&quot; : 2,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;SECONDARY&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 1,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199172, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDurable&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199172, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;2017-12-02T07:19:32Z&quot;),</span><br><span class="line">                        &quot;optimeDurableDate&quot; : ISODate(&quot;2017-12-02T07:19:32Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeat&quot; : ISODate(&quot;2017-12-02T07:20:20.946Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeatRecv&quot; : ISODate(&quot;2017-12-02T07:19:38.290Z&quot;),</span><br><span class="line">                        &quot;pingMs&quot; : NumberLong(0),</span><br><span class="line">                        &quot;configVersion&quot; : 3</span><br><span class="line">                &#125;</span><br><span class="line">        ],</span><br><span class="line">        &quot;ok&quot; : 1</span><br></pre></td></tr></table></figure>
<p>如果是复制集中的主节点挂了，则复制集会剔除这个主节点，并且在复制集中重新选取主节点<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">rs1:SECONDARY&gt; rs.status()</span><br><span class="line">&#123;</span><br><span class="line">        &quot;set&quot; : &quot;rs1&quot;,</span><br><span class="line">        &quot;date&quot; : ISODate(&quot;2017-12-02T07:24:01.177Z&quot;),</span><br><span class="line">        &quot;myState&quot; : 1,</span><br><span class="line">        &quot;term&quot; : NumberLong(2),</span><br><span class="line">        &quot;heartbeatIntervalMillis&quot; : NumberLong(2000),</span><br><span class="line">        &quot;optimes&quot; : &#123;</span><br><span class="line">                &quot;lastCommittedOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199435, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(2)</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;appliedOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199435, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(2)</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;durableOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199435, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(2)</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;members&quot; : [</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 0,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50002&quot;,</span><br><span class="line">                        &quot;health&quot; : 0,</span><br><span class="line">                        &quot;state&quot; : 8,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;(not reachable/healthy)&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 0,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(0, 0),</span><br><span class="line">                                &quot;t&quot; : NumberLong(-1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDurable&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(0, 0),</span><br><span class="line">                                &quot;t&quot; : NumberLong(-1)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;1970-01-01T00:00:00Z&quot;),</span><br><span class="line">                        &quot;optimeDurableDate&quot; : ISODate(&quot;1970-01-01T00:00:00Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeat&quot; : ISODate(&quot;2017-12-02T07:23:55.327Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeatRecv&quot; : ISODate(&quot;2017-12-02T07:23:42.972Z&quot;),</span><br><span class="line">                        &quot;pingMs&quot; : NumberLong(0),</span><br><span class="line">                        &quot;lastHeartbeatMessage&quot; : &quot;Couldn&apos;t get a connection within the time limit&quot;,</span><br><span class="line">                        &quot;configVersion&quot; : -1</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 1,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50001&quot;,</span><br><span class="line">                        &quot;health&quot; : 1,</span><br><span class="line">                        &quot;state&quot; : 1,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;PRIMARY&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 1578,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199435, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(2)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;2017-12-02T07:23:55Z&quot;),</span><br><span class="line">                        &quot;electionTime&quot; : Timestamp(1512199434, 1),</span><br><span class="line">                        &quot;electionDate&quot; : ISODate(&quot;2017-12-02T07:23:54Z&quot;),</span><br><span class="line">                        &quot;configVersion&quot; : 3,</span><br><span class="line">                        &quot;self&quot; : true</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 2,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50003&quot;,</span><br><span class="line">                        &quot;health&quot; : 1,</span><br><span class="line">                        &quot;state&quot; : 2,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;SECONDARY&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 220,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199435, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(2)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDurable&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199435, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(2)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;2017-12-02T07:23:55Z&quot;),</span><br><span class="line">                        &quot;optimeDurableDate&quot; : ISODate(&quot;2017-12-02T07:23:55Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeat&quot; : ISODate(&quot;2017-12-02T07:24:00.036Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeatRecv&quot; : ISODate(&quot;2017-12-02T07:24:00.745Z&quot;),</span><br><span class="line">                        &quot;pingMs&quot; : NumberLong(0),</span><br><span class="line">                        &quot;syncingTo&quot; : &quot;YZJC1606-0218:50001&quot;,</span><br><span class="line">                        &quot;configVersion&quot; : 3</span><br><span class="line">                &#125;</span><br><span class="line">        ],</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br><span class="line">rs1:PRIMARY&gt;</span><br></pre></td></tr></table></figure></p>
<p>等到之前挂掉的主节点故障排除重启后，会加入到复制集成为一个从节点<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">rs1:PRIMARY&gt; rs.status()</span><br><span class="line">&#123;</span><br><span class="line">        &quot;set&quot; : &quot;rs1&quot;,</span><br><span class="line">        &quot;date&quot; : ISODate(&quot;2017-12-02T07:26:20.741Z&quot;),</span><br><span class="line">        &quot;myState&quot; : 1,</span><br><span class="line">        &quot;term&quot; : NumberLong(2),</span><br><span class="line">        &quot;heartbeatIntervalMillis&quot; : NumberLong(2000),</span><br><span class="line">        &quot;optimes&quot; : &#123;</span><br><span class="line">                &quot;lastCommittedOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199575, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(2)</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;appliedOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199575, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(2)</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;durableOpTime&quot; : &#123;</span><br><span class="line">                        &quot;ts&quot; : Timestamp(1512199575, 1),</span><br><span class="line">                        &quot;t&quot; : NumberLong(2)</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;members&quot; : [</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 0,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50002&quot;,</span><br><span class="line">                        &quot;health&quot; : 1,</span><br><span class="line">                        &quot;state&quot; : 2,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;SECONDARY&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 27,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199575, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(2)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDurable&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199575, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(2)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;2017-12-02T07:26:15Z&quot;),</span><br><span class="line">                        &quot;optimeDurableDate&quot; : ISODate(&quot;2017-12-02T07:26:15Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeat&quot; : ISODate(&quot;2017-12-02T07:26:19.407Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeatRecv&quot; : ISODate(&quot;2017-12-02T07:26:19.407Z&quot;),</span><br><span class="line">                        &quot;pingMs&quot; : NumberLong(0),</span><br><span class="line">                        &quot;syncingTo&quot; : &quot;YZJC1606-0218:50003&quot;,</span><br><span class="line">                        &quot;configVersion&quot; : 3</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 1,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50001&quot;,</span><br><span class="line">                        &quot;health&quot; : 1,</span><br><span class="line">                        &quot;state&quot; : 1,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;PRIMARY&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 1717,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199575, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(2)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;2017-12-02T07:26:15Z&quot;),</span><br><span class="line">                        &quot;electionTime&quot; : Timestamp(1512199434, 1),</span><br><span class="line">                        &quot;electionDate&quot; : ISODate(&quot;2017-12-02T07:23:54Z&quot;),</span><br><span class="line">                        &quot;configVersion&quot; : 3,</span><br><span class="line">                        &quot;self&quot; : true</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                        &quot;_id&quot; : 2,</span><br><span class="line">                        &quot;name&quot; : &quot;YZJC1606-0218:50003&quot;,</span><br><span class="line">                        &quot;health&quot; : 1,</span><br><span class="line">                        &quot;state&quot; : 2,</span><br><span class="line">                        &quot;stateStr&quot; : &quot;SECONDARY&quot;,</span><br><span class="line">                        &quot;uptime&quot; : 359,</span><br><span class="line">                        &quot;optime&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199575, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(2)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDurable&quot; : &#123;</span><br><span class="line">                                &quot;ts&quot; : Timestamp(1512199575, 1),</span><br><span class="line">                                &quot;t&quot; : NumberLong(2)</span><br><span class="line">                        &#125;,</span><br><span class="line">                        &quot;optimeDate&quot; : ISODate(&quot;2017-12-02T07:26:15Z&quot;),</span><br><span class="line">                        &quot;optimeDurableDate&quot; : ISODate(&quot;2017-12-02T07:26:15Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeat&quot; : ISODate(&quot;2017-12-02T07:26:20.050Z&quot;),</span><br><span class="line">                        &quot;lastHeartbeatRecv&quot; : ISODate(&quot;2017-12-02T07:26:18.766Z&quot;),</span><br><span class="line">                        &quot;pingMs&quot; : NumberLong(0),</span><br><span class="line">                        &quot;syncingTo&quot; : &quot;YZJC1606-0218:50001&quot;,</span><br><span class="line">                        &quot;configVersion&quot; : 3</span><br><span class="line">                &#125;</span><br><span class="line">        ],</span><br><span class="line">        &quot;ok&quot; : 1</span><br><span class="line">&#125;</span><br><span class="line">rs1:PRIMARY&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="7、添加仲裁节点"><a href="#7、添加仲裁节点" class="headerlink" title="7、添加仲裁节点"></a>7、添加仲裁节点</h2><p>关于仲裁节点在复制集中的关系请参考文档</p>
<p>仲裁节点的添加步骤和创建复制集节点一致。添加仲裁节点数据存储路径，以及日志存储路径，并且添加配置</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">D:\Program Soft\MongoDB\replicset\db_rs3\data</span><br><span class="line">D:\Program Soft\MongoDB\replicset\db_rs3\log</span><br><span class="line"></span><br><span class="line">#创建配置文件和日志文件</span><br><span class="line">D:\Program Soft\MongoDB\replicset\db_rs3\rs0_3.conf</span><br><span class="line">D:\Program Soft\MongoDB\replicset\db_rs3\log\rs0_3.log</span><br></pre></td></tr></table></figure>
<p>配置文件内容，与其他节点配置文件类似<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">dbpath = D:\Program Soft\MongoDB\replicset\db_rs3\data</span><br><span class="line">logpath = D:\Program Soft\MongoDB\replicset\db_rs3\log\rs0_3.log</span><br><span class="line">journal = true</span><br><span class="line">port = 40003</span><br><span class="line">replSet = rs0</span><br></pre></td></tr></table></figure></p>
<h3 id="启动仲裁节点实例"><a href="#启动仲裁节点实例" class="headerlink" title="启动仲裁节点实例"></a>启动仲裁节点实例</h3><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">mongod -config &quot;D:\Program Soft\MongoDB\replicset\db_rs3\rs0_3.conf&quot;</span><br></pre></td></tr></table></figure>
<h3 id="将仲裁节点添加到复制集"><a href="#将仲裁节点添加到复制集" class="headerlink" title="将仲裁节点添加到复制集"></a>将仲裁节点添加到复制集</h3><p>在复制集主节点上执行如下操作<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">rs.addArb(&quot;YZJC1606-0218:40003&quot;)</span><br></pre></td></tr></table></figure></p>
<h2 id="8、更换复制集节点名称"><a href="#8、更换复制集节点名称" class="headerlink" title="8、更换复制集节点名称"></a>8、更换复制集节点名称</h2><p>默认复制集节点名称为【计算机名:port】<br>使用reconfig()方法可以更换复制集的配置信息<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">cfg = rs.conf()</span><br><span class="line">cfg.members[0].host = &quot;VICTOR-PC:40004&quot;</span><br><span class="line">rs.reconfig(cfg)</span><br></pre></td></tr></table></figure></p>
<h3 id="复制集的操作会计入到journal日志中，在复制集启动时会从该日志中读取复制集的状态。"><a href="#复制集的操作会计入到journal日志中，在复制集启动时会从该日志中读取复制集的状态。" class="headerlink" title="复制集的操作会计入到journal日志中，在复制集启动时会从该日志中读取复制集的状态。"></a>复制集的操作会计入到journal日志中，在复制集启动时会从该日志中读取复制集的状态。</h3><blockquote>
<p>参考<br><a href="https://docs.mongodb.com/manual/replication/" target="_blank" rel="external">MongoDB官方文档</a></p>
</blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/Mongo复制集/" rel="tag"># Mongo复制集</a>
          
            <a href="/blog/tags/Mongo/" rel="tag"># Mongo</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/11/30/ubuntu配置Tomcat/" rel="next" title="ubuntu配置Tomcat">
                <i class="fa fa-chevron-left"></i> ubuntu配置Tomcat
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2017/12/04/Mongo复制集二/" rel="prev" title="Mongo复制集二">
                Mongo复制集二 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNjY5NS8xMzIzMA=="></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/head.jpg"
               alt="Fang ShuHao" />
          <p class="site-author-name" itemprop="name">Fang ShuHao</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives">
                <span class="site-state-item-count">135</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories">
                <span class="site-state-item-count">35</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/blog/tags">
                <span class="site-state-item-count">93</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1、MongoDB-复制集"><span class="nav-number">1.</span> <span class="nav-text">1、MongoDB 复制集</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#MongoDB复制是将数据同步在多个服务器的过程。"><span class="nav-number">1.0.0.0.1.</span> <span class="nav-text">MongoDB复制是将数据同步在多个服务器的过程。</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#复制提供了数据的冗余备份，并在多个服务器上存储数据副本，提高了数据的可用性，-并可以保证数据的安全性。"><span class="nav-number">1.0.0.0.2.</span> <span class="nav-text">复制提供了数据的冗余备份，并在多个服务器上存储数据副本，提高了数据的可用性， 并可以保证数据的安全性。</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#复制还允许您从硬件故障和服务中断中恢复数据。"><span class="nav-number">1.0.0.0.3.</span> <span class="nav-text">复制还允许您从硬件故障和服务中断中恢复数据。</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2、什么是复制集"><span class="nav-number">2.</span> <span class="nav-text">2、什么是复制集</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#保障数据的安全性"><span class="nav-number">2.0.0.0.1.</span> <span class="nav-text">保障数据的安全性</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#数据高可用性"><span class="nav-number">2.0.0.0.2.</span> <span class="nav-text">数据高可用性</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#灾难恢复"><span class="nav-number">2.0.0.0.3.</span> <span class="nav-text">灾难恢复</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#无需停机维护（如备份，重建索引，压缩）"><span class="nav-number">2.0.0.0.4.</span> <span class="nav-text">无需停机维护（如备份，重建索引，压缩）</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#分布式读取数据"><span class="nav-number">2.0.0.0.5.</span> <span class="nav-text">分布式读取数据</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3、MongoDB复制集原理"><span class="nav-number">3.</span> <span class="nav-text">3、MongoDB复制集原理</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#mongodb的复制至少需要两个节点。其中一个是主节点，负责处理客户端请求，其余的都是从节点，负责复制主节点上的数据。"><span class="nav-number">3.0.0.0.1.</span> <span class="nav-text">mongodb的复制至少需要两个节点。其中一个是主节点，负责处理客户端请求，其余的都是从节点，负责复制主节点上的数据。</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#mongodb各个节点常见的搭配方式为：一主一从、一主多从。"><span class="nav-number">3.0.0.0.2.</span> <span class="nav-text">mongodb各个节点常见的搭配方式为：一主一从、一主多从。</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#主节点记录在其上的所有操作oplog，从节点定期轮询主节点获取这些操作，然后对自己的数据副本执行这些操作，从而保证从节点的数据与主节点一致。"><span class="nav-number">3.0.0.0.3.</span> <span class="nav-text">主节点记录在其上的所有操作oplog，从节点定期轮询主节点获取这些操作，然后对自己的数据副本执行这些操作，从而保证从节点的数据与主节点一致。</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4、MongoDB复制集特征"><span class="nav-number">4.</span> <span class="nav-text">4、MongoDB复制集特征</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5、复制集配置-（window64环境-Mongo3-4）"><span class="nav-number">5.</span> <span class="nav-text">5、复制集配置 （window64环境 ~ Mongo3.4）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-新建复制集目录"><span class="nav-number">5.1.</span> <span class="nav-text">5.1 新建复制集目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-建立数据目录-以及日志目录"><span class="nav-number">5.2.</span> <span class="nav-text">5.2 建立数据目录 以及日志目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-新建配置文件"><span class="nav-number">5.3.</span> <span class="nav-text">5.3 新建配置文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6、启动复制集"><span class="nav-number">6.</span> <span class="nav-text">6、启动复制集</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-启动所有Mongo实例"><span class="nav-number">6.1.</span> <span class="nav-text">6.1 启动所有Mongo实例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#注意"><span class="nav-number">6.1.1.</span> <span class="nav-text">注意</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#启动Mongo实例时，需要制定复制集参数-replSet-rsName-在配置文件中设置"><span class="nav-number">6.1.2.</span> <span class="nav-text">启动Mongo实例时，需要制定复制集参数-replSet rsName(在配置文件中设置)</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-启动实例客户端"><span class="nav-number">6.2.</span> <span class="nav-text">6.2 启动实例客户端</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#注意-1"><span class="nav-number">6.3.</span> <span class="nav-text">注意</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-添加、删除从节点"><span class="nav-number">6.4.</span> <span class="nav-text">6.2 添加、删除从节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#注意-2"><span class="nav-number">6.5.</span> <span class="nav-text">注意</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-3-在从节点中设置slaveOK"><span class="nav-number">6.6.</span> <span class="nav-text">6.3 在从节点中设置slaveOK</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#主节点停机的情况："><span class="nav-number">6.7.</span> <span class="nav-text">主节点停机的情况：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7、添加仲裁节点"><span class="nav-number">7.</span> <span class="nav-text">7、添加仲裁节点</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#启动仲裁节点实例"><span class="nav-number">7.1.</span> <span class="nav-text">启动仲裁节点实例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#将仲裁节点添加到复制集"><span class="nav-number">7.2.</span> <span class="nav-text">将仲裁节点添加到复制集</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8、更换复制集节点名称"><span class="nav-number">8.</span> <span class="nav-text">8、更换复制集节点名称</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#复制集的操作会计入到journal日志中，在复制集启动时会从该日志中读取复制集的状态。"><span class="nav-number">8.1.</span> <span class="nav-text">复制集的操作会计入到journal日志中，在复制集启动时会从该日志中读取复制集的状态。</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fang ShuHao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  



  
  

  

  

  

  


  

</body>
</html>
